<template>
  <!-- 学习教育 -->
  <base-breadcrumb class="studyEduc-container">
    <searchData :searchData="searchArr" @search="handleSearch"></searchData>
    <a-card :bordered="false">
      <!-- <title-name title="学习教育列表" /> -->
      <div class="pub-table-action-btn">
        <a-button v-btnPermission="'pa_010_add_01'" type="primary" @click="handleUpdate({}, 'add')" v-if="isPartyManager">新增</a-button>
      </div>
      <div class="pub-table-select-count" style="margin: 10px 0">
        <a-icon type="info-circle" theme="twoTone" />
        <span>
          共计：
          <a>{{ totalInfo.totalNum || 0 }} 个</a>
        </span>
      </div>
      <list-table
        ref="table"
        rowKey="id"
        :columns="columns"
        :request="loadData"
        show-size-changer
        show-quick-jumper
        showPagination="auto"
      >
        <template slot="orgLevel" slot-scope="text, record">
          {{ filterLevel(text) }}
        </template>
        <template slot="status" slot-scope="text, record">
          {{ filterStatus(text) }}
        </template>
        <template slot="action" slot-scope="text, record">
          <MoreButton>
            <a v-btnPermission="'pa_010_loo_04'" @click="handleUpdate(record, 'look')">查看</a>
            <a v-btnPermission="'pa_010_edi_02'" v-if="record.status == 2 && record.updateAndDelete" @click="handleUpdate(record, 'edit')">编辑</a>
            <a v-btnPermission="'pa_010_del_03'" v-if="record.status == 2 && record.updateAndDelete" @click="handleUpdate(record, 'delete')">删除</a>
          </MoreButton>
        </template>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
import { STable } from '@/components' // 表格
import { getList, deleteData } from '@/api/partyMasses/partyManage/studyEduc.js'
import { getCurrentPartyLevel } from '@/api/partyMasses'
import { packageConfirm } from '@/utils/baseAction.js'
import { PARTY_TYPE_MAP } from '@/config/businessConfig.js'

const searchArr = [
  {
    name: '学习教育名称',
    value: 'educationName',
    type: 'input'
  },
  {
    name: '发起组织',
    value: 'partyOrgName',
    type: 'input'
  },
  {
    name: '级别',
    value: 'orgLevel',
    type: 'select',
    data: Object.keys(PARTY_TYPE_MAP).map((key) => {
      return {
        value: key,
        label: PARTY_TYPE_MAP[key]
      }
    })
  },
  {
    name: '内容摘要',
    value: 'contentAbstract',
    type: 'input'
  },
  {
    name: '发起日期',
    value: 'launchTime',
    type: 'timeRange',
    attr: {
      rangeFormat: ['launchStartTime', 'launchEndTime']
    }
  }
]
const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'index',
    customRender() {
      return arguments[2] + 1
    }
  },
  {
    title: '学习教育名称',
    dataIndex: 'educationName'
  },
  {
    title: '发起组织',
    dataIndex: 'partyOrgName'
  },
  {
    title: '级别',
    dataIndex: 'orgLevel',
    scopedSlots: { customRender: 'orgLevel' }
  },
  {
    title: '内容摘要',
    dataIndex: 'contentAbstract',
    ellipsis: true,
  },
  {
    title: '发起日期',
    dataIndex: 'launchTime'
  },
  {
    title: '创建时间',
    dataIndex: 'createTime'
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '180px',
    align: 'center',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name:'a'+ Date.now(),
  components: {
    STable,
  },
  data() {
    return {
      totalInfo: {
        totalNum: 0
      },
      searchArr,
      columns: columns,
      queryParam: {},
      isPartyManager: false, // 是否为党组织管理员
    }
  },
  created() {
    this.getCurrentPartyLevelReq()
  },
  methods: {
    /**
     * 获取当前登录人的党组织管理等级
     * 如果返回值中有数组不为空，则是管理人员
     */
    async getCurrentPartyLevelReq() {
      try {
        const res = await getCurrentPartyLevel()
        this.isPartyManager = Object.keys(res.data).some(key => res.data[key] && res.data[key].length)
      } catch(e) {
        console.error(e)
      }
    },
    filterLevel(level) {
      return PARTY_TYPE_MAP[level] || ''
    },
    filterStatus(status) {
      const map = {
        1: '已发送',
        2: '未发送'
      }
      return map[status] || ''
    },
    /**
     * 处理数据的相关操作（增删改查）
     */
    handleUpdate(data, pageType) {
      const commonHandler = () => {
        this.$router.push({
          path: '/partyMasses/partyManage/studyEduc/update',
          query: {
            id: data.id || '',
            pageType: pageType
          }
        })
      }
      const actionMap = {
        add: commonHandler,
        edit: commonHandler,
        look: commonHandler,
        delete: async () => {
          packageConfirm({
            callback: async () => {
              const res = await deleteData({ id: data.id })
              this.$message.success('删除成功')
              this.refreshTable()
            }
          })
        }
      }
      actionMap[pageType] && actionMap[pageType]()
    },
    handleSearch(val = {}) {
      this.queryParam = {
        ...val
      }
      this.refreshTable()
    },
    refreshTable() {
      this.$refs.table.refresh(true)
    },
    async loadData(parameter) {
      const requestParameters = Object.assign({}, { ...parameter }, { query: { ...this.queryParam } })
      return getList(requestParameters).then((res) => {
        this.totalInfo.totalNum = res.totalSize
        return {
          code:200,
          data: res.data,
          currentPage: res.currentPage,
          totalSize: res.totalSize
        }
      })
    }
  }
}
</script>

<style>
</style>
